/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * dutyPersonTableDialog.java
 *
 * Created on 2011-6-1, 10:04:59
 */
package junchangmis.teacher;

import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import junchangmis.*;
import jxl.write.WriteException;
import publicmethod.*;
import javax.swing.JOptionPane;
import java.sql.ResultSet;
import java.util.Vector;

/**
 *
 * @author developers
 */
public class dutyPersonTableDialog extends javax.swing.JDialog {

    MainFrame parent;
    Vector<teacher> tea = new Vector();

    /** Creates new form dutyPersonTableDialog */
    public dutyPersonTableDialog(java.awt.Frame parent, boolean modal, MainFrame pparent) {
        super(parent, modal);
        this.parent = pparent;
        initComponents();
        this.setTitle("教师选择");
        this.setLocationRelativeTo(null);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jComboBoxYearStart = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jComboBoxMonthStart = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jComboBoxYearEnd = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jComboBoxMonthEnd = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextFieldName = new javax.swing.JTextField();
        jButtonConfirm = new javax.swing.JButton();
        jCheckBoxPrint = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("华文细黑", 0, 24));
        jLabel1.setText("起始时间：");

        jComboBoxYearStart.setFont(new java.awt.Font("华文细黑", 0, 24));
        jComboBoxYearStart.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020" }));

        jLabel3.setFont(new java.awt.Font("华文细黑", 0, 24));
        jLabel3.setText("年");

        jComboBoxMonthStart.setFont(new java.awt.Font("华文细黑", 0, 24)); // NOI18N
        jComboBoxMonthStart.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" }));

        jLabel4.setFont(new java.awt.Font("华文细黑", 0, 24));
        jLabel4.setText("月");

        jLabel2.setFont(new java.awt.Font("华文细黑", 0, 24));
        jLabel2.setText("终止时间：");

        jComboBoxYearEnd.setFont(new java.awt.Font("华文细黑", 0, 24));
        jComboBoxYearEnd.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020" }));

        jLabel6.setFont(new java.awt.Font("华文细黑", 0, 24));
        jLabel6.setText("年");

        jComboBoxMonthEnd.setFont(new java.awt.Font("华文细黑", 0, 24)); // NOI18N
        jComboBoxMonthEnd.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" }));

        jLabel7.setFont(new java.awt.Font("华文细黑", 0, 24));
        jLabel7.setText("月");

        jLabel5.setFont(new java.awt.Font("华文细黑", 0, 24));
        jLabel5.setText("教师姓名：");

        jTextFieldName.setFont(new java.awt.Font("华文细黑", 0, 24));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jComboBoxYearEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jComboBoxMonthEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel7))
                            .addComponent(jTextFieldName, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBoxYearStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBoxMonthStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4)))
                .addContainerGap(18, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextFieldName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBoxYearStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jComboBoxMonthStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jComboBoxYearEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(jComboBoxMonthEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addGap(124, 124, 124))
        );

        jButtonConfirm.setFont(new java.awt.Font("华文细黑", 0, 24));
        jButtonConfirm.setText("确定");
        jButtonConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConfirmActionPerformed(evt);
            }
        });

        jCheckBoxPrint.setFont(new java.awt.Font("华文细黑", 0, 24));
        jCheckBoxPrint.setText("是否打印");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jButtonConfirm)
                    .addComponent(jCheckBoxPrint)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBoxPrint)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                .addComponent(jButtonConfirm)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    boolean isBlank(String str) {
        if (str == null || str.trim().equals("")) {
            return true;
        } else {
            return false;
        }
    }
    private void jButtonConfirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonConfirmActionPerformed
        // TODO add your handling code here:        
        if (isBlank(this.jTextFieldName.getText()) == true) {
            JOptionPane.showConfirmDialog(null, "教师姓名不能为空！", "提示", JOptionPane.CLOSED_OPTION, JOptionPane.WARNING_MESSAGE);
        } else {
            try {
                String name = this.jTextFieldName.getText();
                String yearStart = this.jComboBoxYearStart.getSelectedItem().toString();
                String monthStart = this.jComboBoxMonthStart.getSelectedItem().toString();
                String yearEnd = this.jComboBoxYearEnd.getSelectedItem().toString();
                String monthEnd = this.jComboBoxMonthEnd.getSelectedItem().toString();

                DbOperation db = new DbOperation();
                db.DbConnect();
                String sql = "select * from teacher where name='" + name + "' order by monthStart";
                ResultSet rs = db.DBSqlQuery(sql);
                while (rs.next()) {
                    String year = rs.getString("yearStart");
                    String month = rs.getString("monthStart");
                    if (yearStart.equals(yearEnd) == false) {
                        if (yearStart.compareTo(year) <= 0 && yearEnd.compareTo(year) >= 0) {
                            if ((yearStart.equals(year) && monthStart.compareTo(month) > 0) || (yearEnd.equals(year)) && monthEnd.compareTo(month) < 0) {
                                continue;
                            } else {
                                teacher temp = new teacher();
                                //    temp.yearStart = rs.getString("yearStart");
                                //  temp.monthStart = rs.getString("monthStart");
                                temp.yearStart = year;
                                temp.monthStart = month;
                                temp.dayStart = rs.getString("dayStart");
                                temp.yearEnd = rs.getString("yearEnd");
                                temp.monthEnd = rs.getString("monthEnd");
                                temp.dayEnd = rs.getString("dayEnd");
                                temp.week = rs.getString("week");
                                temp.name = rs.getString("name");
                                temp.classCode = rs.getString("classCode");
                                temp.classCount = rs.getString("classCount");
                                temp.listenCount = rs.getString("listenCount");
                                temp.sumStart = rs.getString("sumStart");
                                temp.sumEnd = rs.getString("sumEnd");
                                temp.change = rs.getString("change");
                                temp.stopCount = rs.getString("stopCount");
                                temp.shiftCount = rs.getString("shiftCount");
                                temp.insteadCount = rs.getString("insteadCount");
                                temp.addCount = rs.getString("addCount");

                                tea.add(temp);
                            }
                        }
                    } else {
                        if (yearStart.equals(year)) {
                            if (monthStart.compareTo(month) <= 0 && monthEnd.compareTo(month) >= 0) {
                                teacher temp = new teacher();
                                // temp.yearStart = rs.getString("yearStart");
                                //temp.monthStart = rs.getString("monthStart");
                                temp.yearStart = year;
                                temp.monthStart = month;
                                temp.dayStart = rs.getString("dayStart");
                                temp.yearEnd = rs.getString("yearEnd");
                                temp.monthEnd = rs.getString("monthEnd");
                                temp.dayEnd = rs.getString("dayEnd");
                                temp.week = rs.getString("week");
                                temp.name = rs.getString("name");
                                temp.classCode = rs.getString("classCode");
                                temp.classCount = rs.getString("classCount");
                                temp.listenCount = rs.getString("listenCount");
                                temp.sumStart = rs.getString("sumStart");
                                temp.sumEnd = rs.getString("sumEnd");
                                temp.change = rs.getString("change");
                                temp.stopCount = rs.getString("stopCount");
                                temp.shiftCount = rs.getString("shiftCount");
                                temp.insteadCount = rs.getString("insteadCount");
                                temp.addCount = rs.getString("addCount");

                                tea.add(temp);
                            }
                        }
                    }
                }
                db.DbClose();
            } catch (SQLException ex) {
                Logger.getLogger(dutyPersonTableDialog.class.getName()).log(Level.SEVERE, null, ex);
            }
            if (tea.size() > 0) {
                try {
                    String title = this.jTextFieldName.getText() + " " + this.jComboBoxYearStart.getSelectedItem().toString() + "." + this.jComboBoxMonthStart.getSelectedItem().toString() + "-" + this.jComboBoxYearEnd.getSelectedItem().toString() + "." + this.jComboBoxMonthEnd.getSelectedItem().toString();
                    dutyPersonTableExcel work = new dutyPersonTableExcel(tea, title);
                    String filename = "教师考勤考核表\\";
                    filename = filename + this.jTextFieldName.getText() + "(" + this.jComboBoxYearStart.getSelectedItem().toString() + "." + this.jComboBoxMonthStart.getSelectedItem().toString() + "-" + this.jComboBoxYearEnd.getSelectedItem().toString() + "." + this.jComboBoxMonthEnd.getSelectedItem().toString() + ")" + "考勤考核表.xls";
                    work.setOutputFile(filename);
                    work.write();
                    System.out.println("Please check the result file " + filename);
                    JOptionPane.showConfirmDialog(null, "生成成功！", "提示", JOptionPane.CLOSED_OPTION, JOptionPane.INFORMATION_MESSAGE);
                    if (this.jCheckBoxPrint.isSelected() == true) {
                        PrintTest.print(PrintTest.getPath() + "\\" + filename);
                    } else {
                        PrintTest.showTable(PrintTest.getPath() + "\\" + filename);
                    }
                    dispose();
                } catch (WriteException ex) {
                    Logger.getLogger(dutyPersonTableDialog.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(dutyPersonTableDialog.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else {
                JOptionPane.showConfirmDialog(null, "没有记录！", "提示", JOptionPane.CLOSED_OPTION, JOptionPane.WARNING_MESSAGE);
            }
        }
    }//GEN-LAST:event_jButtonConfirmActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* java.awt.EventQueue.invokeLater(new Runnable() {
        
        public void run() {
        dutyPersonTableDialog dialog = new dutyPersonTableDialog(new javax.swing.JFrame(), true);
        dialog.addWindowListener(new java.awt.event.WindowAdapter() {
        
        public void windowClosing(java.awt.event.WindowEvent e) {
        System.exit(0);
        }
        });
        dialog.setVisible(true);
        }
        });*/
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonConfirm;
    private javax.swing.JCheckBox jCheckBoxPrint;
    private javax.swing.JComboBox jComboBoxMonthEnd;
    private javax.swing.JComboBox jComboBoxMonthStart;
    private javax.swing.JComboBox jComboBoxYearEnd;
    private javax.swing.JComboBox jComboBoxYearStart;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextFieldName;
    // End of variables declaration//GEN-END:variables
}
